from django.http import Http404
from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404
from django.template import RequestContext
from django.conf import settings
from django.core.paginator import Paginator


from bl_g.models import Entry, Category

class AtomFeed:
    title = None
    url = None
    
def main(request, template_name='bl_g/page.html'):
    """
    The main page of the blog
    
    """
    tmp = Entry.published_objects.all()
    entries = []
    for entry in tmp:
        entries.append({'entry':entry, 'comment_count':'?',})
    paginator = Paginator(entries, settings.BL_G_PAGE_SIZE)
    
    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1

    # If page request (9999) is out of range, deliver last page of results.
    try:
        entries = paginator.page(page)
    except (EmptyPage, InvalidPage):
        entries = paginator.page(paginator.num_pages)
        
    if page == 1:
        nav_page = 'bl_g_main'
    else:
        nav_page = None
    return render_to_response(template_name, {'entries':entries, 'section':'blog', 'title': settings.BL_G_TITLE, 'nav_page':nav_page},
                              context_instance=RequestContext(request))

def category(request, slug, template_name='bl_g/page.html'):
    """
    A category page of the blog
    
    """
    tmp = Entry.published_objects.filter(category__slug=slug)
    entries = []
    for entry in tmp:
        entries.append({'entry':entry, 'comment_count':'?',})
    paginator = Paginator(entries, settings.BL_G_PAGE_SIZE)
    
    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1

    # If page request (9999) is out of range, deliver last page of results.
    try:
        entries = paginator.page(page)
    except (EmptyPage, InvalidPage):
        entries = paginator.page(paginator.num_pages)
    
    category = get_object_or_404(Category, slug=slug)
    atom_feed = AtomFeed()
    atom_feed.title = settings.BL_G_CATEGORY_PATTERN % category
    if category.feedburner_url:
        atom_feed.url = category.feedburner_url
    else:
        atom_feed.url = '/feeds/atom-categories/%s' % category.slug #FIXME
    
    return render_to_response(template_name, {'entries':entries, 'title': category, 'atom_feed': atom_feed, 'section':'blog',},
                              context_instance=RequestContext(request))
    
def index(request, template_name='bl_g/index.html'):
    """
    The index page of the blog, listing all entries
    
    """
    tmp = Entry.published_objects.all()
    # to correspond to data format of main
    entries = []
    for entry in tmp:
        entries.append({'entry':entry,})
    return render_to_response(template_name, {'entries':entries, 'section':'blog', },
                              context_instance=RequestContext(request))  

def entry(request, slug, template_name="bl_g/entry.html"):
    """
    A single entry in the blog
    """
    try:
        tmp = Entry.published_objects.get(slug=slug)
    except Entry.DoesNotExist:
        raise Http404
    entry = {'entry':tmp, 'comment_count':'?',}
    return render_to_response(template_name, {'entry':entry, 'section':'blog',},
                              context_instance=RequestContext(request))
